const Controller = require('egg').Controller;

const fs = require('fs')

const MockData = require('../../mock')

const JsonData = require('./data.json')

class HomeController extends Controller {
    //往数据库添加数据
    async mock() {
        const { ctx, app } = this

        // fs.writeFileSync(`${__dirname}/data.json`, JSON.stringify(MockData), () => { })

        const data = await app.mysql.insert('直播', (JsonData))

        ctx.body = {
            code: 0,
            msg: '请求成功',
            data
        }
    }

    //获取房源数据
    async housing() {
        const { ctx, app } = this

        const { value, size, current } = ctx.request.body

        try {
            const data = await app.mysql.query(`select * from 二手房 where name like '%${value}%' limit ${size * current}`)

            ctx.body = {
                code: 0,
                msg: '请求成功',
                data
            }
        } catch (error) {
            ctx.body = {
                msg: error
            }
        }
    }

    //房源删除
    async housingDel() {
        const { ctx, app } = this

        const { id } = ctx.request.body;

        await app.mysql.delete('二手房', { id })

        ctx.body = {
            code: 0,
            msg: '请求成功'
        }
    }

    //房源编辑
    async housingEdit() {
        const { ctx, app } = this

        const { name, address, num, date, sort, id } = ctx.request.body

        await app.mysql.update('二手房', { name, address, num, date, sort, id })

        ctx.body = {
            code: 0,
            msg: '请求成功'
        }
    }

    //房源添加
    async housingAdd() {
        const { ctx, app } = this

        const { name, address, num, date, sort } = ctx.request.body

        await app.mysql.insert('二手房', { name, address, num, date, sort })

        ctx.body = {
            code: 0,
            msg: '请求成功'
        }
    }

    //获取直播数据
    async live() {
        const { ctx, app } = this

        const { value } = ctx.request.body

        try {
            const data = await app.mysql.query(`select * from 直播 where name like '%${value}%'`)

            ctx.body = {
                code: 0,
                msg: '请求成功',
                data
            }
        } catch (error) {
            ctx.body = {
                msg: error
            }
        }
    }

    //获取团购数据
    async group() {
        const { ctx, app } = this

        try {
            const data = await app.mysql.query('select * from 团购')

            ctx.body = {
                code: 0,
                msg: '请求成功',
                data
            }
        } catch (error) {
            ctx.body = {
                msg: error
            }
        }
    }

    //三级联动
    async cele() {
        const { ctx, app } = this
        const { value } = ctx.request.body
        console.log(ctx.request.body);
        console.log(value.area.value);
        const data = await app.mysql.query(`select * from 二手房 where address like "%${value.area.value}%"`)
        console.log(data);
        ctx.body = {
            code: 1,
            msg: "请求成功",
            data

        }
    }
}

module.exports = HomeController;